using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Drawing.Printing;using System.Text;using System.Windows.Forms;using PrintControl;namespace PrintDemo{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { dataGridView1.Rows.Clear(); for (int i = 1; i < 150; i ) dataGridView1.Rows.Add(); } private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e) { ControlPrint m_print = new ControlPrint(listBox1); printPreviewDialog1.Document = (PrintDocument)m_print; printPreviewDialog1.ShowDialog(); } private void printToolStripMenuItem_Click(object sender, EventArgs e) { ControlPrint m_print = new ControlPrint(listBox1); m_print.Print(); } private void printPreviewToolStripMenuItem1_Click(object sender, EventArgs e) { ControlPrint m_print = new ControlPrint(); m_print.StretchControl = true; m_print.SetControl(treeView1); m_print.PrintWidth = m_print.CalculateSize().Width; m_print.PrintHeight = m_print.CalculateSize().Height; printPreviewDialog1.Document = (PrintDocument)m_print; printPreviewDialog1.ShowDialog(); } private void printToolStripMenuItem1_Click(object sender, EventArgs e) { ControlPrint m_print = new ControlPrint(treeView1, true); m_print.Print(); } private void printPreviewToolStripMenuItem2_Click(object sender, EventArgs e) { ControlPrint m_print = new ControlPrint(dataGridView1); printPreviewDialog1.Document = (PrintDocument)m_print; printPreviewDialog1.ShowDialog(); } private void printToolStripMenuItem2_Click(object sender, EventArgs e) { ControlPrint m_print = new ControlPrint(listBox1); m_print.Print(); } private void printPreviewToolStripMenuItem3_Click(object sender, EventArgs e) { printPreviewDialog1.Document = printDocument1; printPreviewDialog1.ShowDialog(); } private void printToolStripMenuItem3_Click(object sender, EventArgs e) { printDocument1.Print(); } private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) { ControlPrint L1 = new ControlPrint(listView1); L1.PrintWidth = L1.CalculateSize().Width; L1.PrintHeight = L1.CalculateSize().Height; ControlPrint L2 = new ControlPrint(listView2); L2.PrintWidth = L2.CalculateSize().Width; L2.PrintHeight = L2.CalculateSize().Height; ControlPrint L3 = new ControlPrint(listView3); L3.PrintWidth = L3.CalculateSize().Width; L3.PrintHeight = L3.CalculateSize().Height; e.Graphics.DrawString("This is a sample printing", new Font("Tahoma", 16), Brushes.Black, new PointF(40, 40)); e.Graphics.DrawImageUnscaled(L1.GetBitmap(), new Point(100, 100)); e.Graphics.DrawString("Another sample", new Font("Tahoma", 16), Brushes.Black, new PointF(40, 300)); e.Graphics.DrawImageUnscaled(L2.GetBitmap(), new Point(100, 350)); e.Graphics.DrawString("Another sample", new Font("Tahoma", 16), Brushes.Black, new PointF(40, 500)); e.Graphics.DrawImageUnscaled(L3.GetBitmap(), new Point(100, 550)); } private void printPreviewToolStripMenuItem4_Click(object sender, EventArgs e) { ControlPrint m_print = new ControlPrint(tabControl1.TabPages[4]); printPreviewDialog1.Document = (PrintDocument)m_print; printPreviewDialog1.ShowDialog(); } private void printToolStripMenuItem4_Click(object sender, EventArgs e) { ControlPrint m_print = new ControlPrint(tabControl1.TabPages[4]); m_print.Print(); } private void expandAllToolStripMenuItem_Click(object sender, EventArgs e) { treeView1.ExpandAll(); } }}
评论